import pyqtgraph as pg
import numpy as np
class AccelWidget(pg.GraphicsLayoutWidget):
    def __init__(self,title='Roll 数值曲线',label="角度/°",datSize=500,freq=50):
        super().__init__()
        self.bDat=[]
        self.lDat=[]
        self.rDat=[]
        self.datSize=datSize
        self.freq=freq
        self.stamps=np.linspace(0,datSize/freq,datSize)
        self.title=title
        self.label=label
        self.InitDat()
    def InitDat(self):
        '''
        初始化
        :return:
        '''
        for i in range(self.datSize):
            self.bDat.append(0.0)
            self.lDat.append(0.0)
            self.rDat.append(0.0)
        self.canvas=self.addPlot(title=self.title)
        self.canvas.setLabel('left', self.label)
        self.canvas.addLegend()
        self.bCurve=self.canvas.plot(pen="r",name="头")
        self.bCurve.setData(self.stamps, self.lDat)

        self.lCurve = self.canvas.plot(pen="b", name="左")
        self.lCurve.setData(self.stamps, self.lDat)
        self.rCurve = self.canvas.plot(pen="y", name="右")
        self.rCurve.setData(self.stamps, self.lDat)


    def Feed(self,l,r,b):
        '''
        数据输入
        :param l:
        :param r:
        :param b:
        :return:
        '''
        self.lDat.append(l);self.lDat.pop(0)
        self.rDat.append(r);self.rDat.pop(0)
        self.bDat.append(b);self.bDat.pop(0)

    def UpdateData(self,l,r,b):
        '''
        更新数据
        :param l:
        :param r:
        :param b:
        :return:
        '''
        self.Feed(l,r,b)
        self.bCurve.setData(self.stamps,self.bDat)
        self.rCurve.setData(self.stamps, self.rDat)
        self.lCurve.setData(self.stamps, self.lDat)



if __name__=='__main__':
    f=AccelWidget()
    pg.exec()


